package com.etc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.json.JSONException;
import org.springframework.stereotype.Service;

import com.etc.dao.PathMapper;
import com.etc.dao.ScenicMapper;
import com.etc.entity.PathAndSelectScenicMessage;
import com.etc.entity.PathAndSelectscenic;
import com.etc.entity.Scenic;
import com.etc.entity.SelectScenicMessage;
import com.etc.entity.Selectscenic;
import com.etc.service.PlayWayService;
import com.etc.service.arithmetic.BuildMatrix;
import com.etc.service.arithmetic.DetailWayMessage;
import com.etc.service.arithmetic.HotelJson;
import com.etc.service.arithmetic.HotelRecommend;
import com.etc.service.arithmetic.ScenicMessage;
import com.etc.service.arithmetic.TakeCatWay;

/**
 * 规划路线业务层实现类
 * @author 小吴
 *
 */
@Service
public class PlayWayServiceImpl implements PlayWayService{
	
	@Resource
	private ScenicMapper smapper;
	@Resource
	private  PathMapper pmapper;
	//判断数据库中是否已经存在该数据
	public  List<PathAndSelectscenic> findPlayWay(int pid,int cid,int hid,int rsort_rule, int[] scenicid){
		//判断数据库中是否已经存在该数据
		boolean falg = false;//数据库中不存在该数据时，该判断为真
		int count[] = {-1,-1};//记录数据库中存在数据的行数。
		int z	= 0;
		List<PathAndSelectscenic> pass = pmapper.selectPathByPidCidAndHid(pid, cid, hid);
		for(int i = 0 ; i < pass.size();i++) {
			List<Selectscenic> ss = pass.get(i).getSs();
			if(ss.size()==scenicid.length) {
				for(int j = 0 ; j < ss.size();j++) {
					int scid = ss.get(j).getSid();
					boolean fl = false;//判断是否存在该景点
					for(int k = 0 ; k < scenicid.length; k++) {
						if(scid == scenicid[k]) {
							fl = true;
						}						
						if(fl) {
							break;
						}
					}
					//一次循环完，还没没有一个匹配则不匹配
					if(!fl) {
						falg = true;//数据库中不存在该数据
						break;
					}
					fl=false;
				}
			}else {
				
				continue;
			}
			 if(!falg) {
				count[z] = i;//保存记录
				z++;
			}
		}
				
		List<PathAndSelectscenic> list = new ArrayList<>();
		for(int i = 0 ; i < count.length&&pass.size()>0;i++) {
			if(rsort_rule==2) {
				if(count[i]!=-1 ) {
					list.add(pass.get(count[i]));	
				}
			}else if(Integer.parseInt(pass.get(count[i]).getPtype())==rsort_rule){
				if(count[i]!=-1 ) {
					list.add(pass.get(count[i]));	
				}
			}			
		}
		return list;
	}
	
	//当用户选择的景点等于1个时
	@Override
	public List<PathAndSelectScenicMessage> playWayOneScenic(int pid,int cid,int hid,int rsort_rule, int[] scenicid) throws JSONException {
		List<PathAndSelectScenicMessage> passm = new ArrayList<>();
		//判断数据库中是否已经存在该数据
		List<PathAndSelectscenic> pass = findPlayWay(pid,cid,hid,rsort_rule,scenicid);
		//数据库中不存在该方案
			HashMap<Integer, Integer> hash = new HashMap<>();
			if(rsort_rule == 2) {
				hash.put(0, 0);
				hash.put(1, 1);
			}else {
				hash.put(rsort_rule,rsort_rule);
			}
			for(int i = 0 ; i < pass.size();i++) {
				int type = Integer.parseInt(pass.get(i).getPtype());
				hash.remove(type);
				
			}
		
			int sid = scenicid[0];
			ScenicMessage sm = smapper.selectScenicBySidfindalitterMessage(sid);
			Scenic scenic = smapper.selectByPrimaryKey(sid);
			for(HashMap.Entry<Integer, Integer> entry : hash.entrySet()) {
				PathAndSelectScenicMessage pm = new PathAndSelectScenicMessage();
				pm.setHid(hid);
				pm.setPprovince(pid);
				pm.setPcity(cid);
				pm.setPtype(entry.getValue()+"");
				SelectScenicMessage ssm = new SelectScenicMessage();				
				HotelJson hj = HotelRecommend.getHotelMessage(sm.getsX(), sm.getsY(), entry.getValue());
				ssm.setSid(sm.getsId());
				ssm.setSsrand(1);
				ssm.setSshotelname(hj.getName());
				ssm.setSshotellon(hj.getLocation().getLng());
				ssm.setSshotellat(hj.getLocation().getLat());
				ssm.setSshotelmoney(hj.getDetail_info().getPrice());
				ssm.setSshotelurl(hj.getDetail_info().getDetail_url());				
				ssm.setSmassage(scenic);
				List<SelectScenicMessage> listssm = new ArrayList<>();
				listssm.add(ssm);
				pm.setPbudget((hj.getDetail_info().getPrice())+scenic.getSticket());//经费
				pm.setSs(listssm);
				passm.add(pm);
			}			
		return passm;
	}

	//当用户选择的景点等于2个时
	@Override
	public List<PathAndSelectScenicMessage> playWayTwoScenic(int pid,int cid,int hid,int rsort_rule, int[] scenicid)throws JSONException {
		//判断数据库中是否已经存在该数据
		List<PathAndSelectscenic> pass = findPlayWay(pid,cid,hid,rsort_rule,scenicid);
		//数据库中不存在该方案				
		List<PathAndSelectScenicMessage> passm = new ArrayList<>();
		
		HashMap<Integer, Integer> hash = new HashMap<>();
		if(rsort_rule == 2) {
			hash.put(0, 0);
			hash.put(1, 1);
		}else {
			hash.put(rsort_rule,rsort_rule);
		}
		
			ScenicMessage sm0 = smapper.selectScenicBySidfindalitterMessage(scenicid[0]);
			ScenicMessage sm1 = smapper.selectScenicBySidfindalitterMessage(scenicid[1]);
			
			TakeCatWay tcw = new TakeCatWay();
			tcw.setStar(sm0.getsName());
			tcw.setStarX(sm0.getsX());
			tcw.setStarY(sm0.getsY());
			tcw.setTime(sm0.getTime());
			tcw.setEnd(sm1.getsName());
			tcw.setEndX(sm1.getsX());
			tcw.setEndY(sm1.getsY());
			tcw = BuildMatrix.getTwoPointsBetweenPoth(tcw);
			List<TakeCatWay> listtcw = new ArrayList<>();
			listtcw.add(tcw);
			listtcw.add(new TakeCatWay(sm1.getsName(), null ,sm1.getsX(),sm1.getsY(), 0, 0,0,0,null,sm1.getTime()));
										
			for(HashMap.Entry<Integer, Integer> entry : hash.entrySet()) {
				PathAndSelectScenicMessage pm = new PathAndSelectScenicMessage();
				pm.setHid(hid);
				pm.setPprovince(pid);
				pm.setPcity(cid);
				pm.setPtype(entry.getValue()+"");
				List<SelectScenicMessage> listssm = new ArrayList<>();
				List<TakeCatWay> list = DetailWayMessage.detailHotel(listtcw, entry.getValue());
				
					for(int j = 0 ; j < list.size();j++) {
						Scenic sc = smapper.selectScenicBySname(list.get(j).getStar());
						SelectScenicMessage ssm = new SelectScenicMessage();
						ssm.setSmassage(sc);
						ssm.setSid(sc.getSid());
						ssm.setSsnextss(list.get(j).getWay());
						if(list.get(j).getHotelJson()!=null) {
						ssm.setSshotelname(list.get(j).getHotelJson().getName());
						ssm.setSshotelurl(list.get(j).getHotelJson().getDetail_info().getDetail_url());
						ssm.setSshotellat(list.get(j).getHotelJson().getLocation().getLat());
						ssm.setSshotellon(list.get(j).getHotelJson().getLocation().getLng());
						ssm.setSshotelmoney(list.get(j).getHotelJson().getDetail_info().getPrice());
						}
						listssm.add(ssm);
					}
					pm.setSs(listssm);
					passm.add(pm);
			}
			
		return passm;
	}

	//当用户选择的景点大于2个时
	@Override
	public List<PathAndSelectScenicMessage> playWayMoreScenic(int pid,int cid,int hid,int rsort_rule, int[] scenicid)throws JSONException {
		//判断数据库中是否存在该方案
		List<PathAndSelectscenic> pass = findPlayWay(pid,cid,hid,rsort_rule,scenicid);
		
		List<PathAndSelectScenicMessage> passm = new ArrayList<>();
		
		HashMap<Integer, Integer> hash = new HashMap<>();
		if(rsort_rule == 2) {
			hash.put(0, 0);
			hash.put(1, 1);
		}else {
			hash.put(rsort_rule,rsort_rule);
		}
		
		List<ScenicMessage> list = new ArrayList<>();
		for(int i = 0 ; i < scenicid.length ; i++) {
			ScenicMessage sm = smapper.selectScenicBySidfindalitterMessage(scenicid[i]);
			list.add(sm);
		}
		//获取最佳路径
		List<TakeCatWay> listTCW = BuildMatrix.planWay(list);
		
		for(HashMap.Entry<Integer, Integer> entry : hash.entrySet()) {
			PathAndSelectScenicMessage pm = new PathAndSelectScenicMessage();
			pm.setHid(hid);
			pm.setPprovince(pid);
			pm.setPcity(cid);
			pm.setPtype(entry.getValue()+"");
			List<SelectScenicMessage> listssm = new ArrayList<>();
			
			List<TakeCatWay> listt = DetailWayMessage.detailHotel(listTCW, entry.getValue());
			for(int j = 0 ; j < listt.size();j++) {
				Scenic sc = smapper.selectScenicBySname(listt.get(j).getStar());
				SelectScenicMessage ssm = new SelectScenicMessage();
				ssm.setSmassage(sc);
				ssm.setSid(sc.getSid());
				ssm.setSsnextss(listt.get(j).getWay());
				if(listt.get(j).getHotelJson()!=null) {
				ssm.setSshotelname(listt.get(j).getHotelJson().getName());
				ssm.setSshotelurl(listt.get(j).getHotelJson().getDetail_info().getDetail_url());
				ssm.setSshotellat(listt.get(j).getHotelJson().getLocation().getLat());
				ssm.setSshotellon(listt.get(j).getHotelJson().getLocation().getLng());
				ssm.setSshotelmoney(listt.get(j).getHotelJson().getDetail_info().getPrice());
				}
				listssm.add(ssm);
			}
			pm.setSs(listssm);
			passm.add(pm);
		
		}
		
		/*if(rsort_rule!=2) {
			List<TakeCatWay> listt =DetailWayMessage.detailHotel(listTCW,rsort_rule);
		}else {
			List<TakeCatWay> listt0 =DetailWayMessage.detailHotel(listTCW,0);
			List<TakeCatWay> listt1 =DetailWayMessage.detailHotel(listTCW,1);
		}*/
		return passm;
	}
	

	//当用户没有确定具体的景点时
	@Override
	public List<PathAndSelectScenicMessage> playWayNoScenic(int pid,int cid,int hid,int rsort_rule)throws JSONException {
		//判断数据库中是否存在该城市的路线
		List<PathAndSelectscenic> pass = pmapper.selectPathByPidCidAndHid(pid, cid, hid);
		
		return null;
	}

}
